package com.sichen.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.sichen.product.vo.SpuSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sichen.product.entity.SpuInfoEntity;
import com.sichen.product.service.SpuInfoService;
import com.sichen.common.utils.PageUtils;
import com.sichen.common.utils.R;



/**
 * spu信息
 *
 * @author sichen
 * @email 15837757959@163.com
 * @date 2022-05-14 14:44:50
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 根据前端传递的条件进行分页条件查询
     * @param params 条件
     * @return 返回的查询结果值
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.queryPageByCondition(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("produck:spuinfo:info")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);
        return R.ok().put("spuInfo", spuInfo);
    }

    // /product/spuinfo/{spuId}/up

    /**
     * 商品上架功能
     * @param spuId spuId
     * @return 操作的结果
     */
    @PostMapping("/{spuId}/up")
    public R spuUp(@PathVariable("spuId") Long spuId){
        spuInfoService.up(spuId);
        return R.ok();
    }


    /**
     * 保存Spu商品信息
     * @param spuSaveVo 前端传递的商品信息
     * @return 保存结果
     */
    @PostMapping("/save")
    //@RequiresPermissions("produck:spuinfo:save")
    public R save(@RequestBody SpuSaveVo spuSaveVo){
        spuInfoService.saveSpuInfo(spuSaveVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("produck:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("produck:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
